(function ($ang) {
    'use strict';
    $ang.module('gtPartyApp').controller('cdlistCtrl', function ($scope, $http, $alert, $modal, AuthUser, $state, $loading) {
        $scope.title = '菜单管理';
        //分页设置
        $scope.selectData = {searchFlmc: '', ptotal: 0};
        //当前用户获取
        $scope.user = AuthUser.getUser();
        //所有的菜单记录
        $scope.cdList = [];

        //查询对应菜单数据
        $scope.Search = function () {
            //$scope.selectData.pageindex=1;
            $scope.LoadData();
        };

        $scope.LoadData = function () {
            $loading.show();
            $http.get('../../api/mh/FindFlList', {params: $scope.selectData}).then(function (req) {
                $loading.hide();
                $scope.cdList = req.data.item;
                $scope.selectData.ptotal = req.data.item.length;
            }, function (err) {
                $loading.hide();
                $alert({title: '错误：', content: '服务器错误', placement: 'top', type: 'info', show: true, duration: 3});
            });
        };

        $scope.loadChildMenu = function (it) {
            it.isExpand = !it.isExpand;
        };

        $scope.LoadData();

        var cdModal = $modal({
            scope: $scope,
            title: '菜单信息',
            templateUrl: 'mh/cd/cdEditModal.html',
            controller: 'cdEditCtrl',
            show: false,
            animation: 'am-fade-and-slide-top'
        });
        $scope.modalData = {};
        $scope.EditModal = function (it) {
            $scope.modalData = it;
            cdModal.$promise.then(cdModal.show);
        };
        $scope.AddModal = function () {
            $scope.modalData = {};
            cdModal.$promise.then(cdModal.show);
        };

        $scope.DelItem = function (id) {
            if (confirm('是否删除分栏？')) {
                $loading.show();
                $http({
                    method: 'get',
                    url: '../../api/mh/DelMhfl',
                    params: {
                        mhflbs: id
                    }
                }).then(function (result) {
                    $scope.Search();
                    $loading.hide();
                }, function (resp) {
                    $loading.hide();
                });
            }
        };


    }).controller('cdEditCtrl', function ($scope, $http, $alert, $loading) {
        $scope.title = '分栏明细单';
        $scope.menu = $scope.$parent.$parent.modalData;
        $scope.ztList = [];
        $scope.viewdata = {
            selectSjflOp: {
                url: '../../api/mh/SelectMhfLParent',
                title: '分栏选择',
                isMulti: false,
                selectData: {flmc: '分栏名称'},
                columns: {flmc: '分栏名称', bz: '级别'},
                selectLabelKey: 'flmc',
                selectValuekey: 'mhflbs',
                selectedValueData: $scope.menu.sjfl,
                selectedLabeData: $scope.menu.sjflName,
                selectedJData: []
            }
        };

        $scope.GetZtData = function () {
            $http({
                method: 'get',
                url: '../../api/dictionary/getDictionaryListByDicTypeKey',
                params: {
                    dicTypeKey: 'ztType'
                }
            }).then(function (result) {
                $scope.ztList = result.data;
            }, function (resp) {

            });
        };

        $scope.init = function () {
            $scope.GetZtData();
            if (!$scope.menu.mhflbs) {
                $scope.menu.sfxs = 1;
                $scope.menu.px = 1;
            }
        };

        $scope.init();

        $scope.SaveMenu = function () {

            if (!this.menuForm.$valid) {
                return;
            }

            $loading.show();
            $http({
                method: 'post',
                url: '../../api/mh/SaveMhfL',
                data: $scope.menu
            }).then(function (req) {
                $loading.hide();
                if (req.data.success) {
                    $scope.$parent.$parent.LoadData();
                    $scope.$hide();
                } else {
                    $alert({
                        title: '错误：',
                        content: req.data.msg,
                        placement: 'top',
                        type: 'info',
                        show: true,
                        duration: 3
                    });
                }

            }, function (err) {
                $loading.hide();
            })

        };

        $scope.SelectParent = function () {
            $scope.menu.sjfl = '';
            $scope.menu.sjflName = '根目录';
        }
    });
})(angular);